package com.oc.mba.controller;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oc.mba.entity.OcDevicePaper;
import com.oc.mba.entity.OcOrder;
import com.oc.mba.service.PaymentService;
import com.oc.mba.util.CommLog;
import com.oc.mba.util.SpringUtil;

public class YoumiNotifyController extends BaseController {
	
	private static final CommLog logger = new CommLog(YoumiNotifyController.class);
	/**
	 * 用户登录的游戏服务器ID 
	 */
	private final String SERVERID_KEY = "serverId";
	/**
	 * 充值订单自定义数据
	 */
	private final String CALLBACKINFO_KEY = "callbackInfo";
	/**
	 * 我方账号唯一标识
	 */
	private final String OPENID_KEY = "openId";
	/**
	 * 订单ID,我方订单系统唯一号
	 */
	private final String ORDERID_KEY = "orderId";
	/**
	 * 订单状态,1为成功
	 */
	private final String ORDERSTATUS_KEY = "orderStatus";
	/**
	 * 充值类型
	 */
	private final String PAYTYPE_KEY = "payType";
	/**
	 * 成功充值金额（人民币，单位为元，浮点数）
	 */
	private final String AMOUNT_KEY = "amount";
	/**
	 * 备注说明,充值失败时描述失败原因
	 */
	private final String REMARK_KEY = "remark";
	/**
	 * 充值回调签名算法
	 */
	private final String SIGN_KEY = "sign";
	
	/**
	 * 分发逻辑
	 * @param request 请求
	 * @param response 响应
	 * @throws Exception 异常
	 */
	public void dispatch(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		logger.info("YoumiNotifyController progress....");
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		String serverId = request.getParameter(SERVERID_KEY);
		String pOrderID = request.getParameter(CALLBACKINFO_KEY);
		String openId = request.getParameter(OPENID_KEY);
		String orderID = request.getParameter(ORDERID_KEY);
		String orderStatus = request.getParameter(ORDERSTATUS_KEY);
		String payType = request.getParameter(PAYTYPE_KEY);
		String amount = request.getParameter(AMOUNT_KEY);
		String remark = request.getParameter(REMARK_KEY);
		String sign = request.getParameter(SIGN_KEY);
		
		logger.info("serverId=" + serverId 
				+ ", callbackInfo=" + pOrderID
				+ ", openId=" + openId
				+ ", orderId=" + orderID
				+ ", orderStatus=" + orderStatus
				+ ", payType=" + payType
				+ ", amount=" + amount
				+ ", remark=" + remark
				+ ", sign=" + sign);
		
		if (pOrderID!=null && !pOrderID.equals("")) {
			PaymentService paymentService = (PaymentService) SpringUtil.getBean("paymentServiceImpl");
			// 群体支付
			if (pOrderID.startsWith("DID-")) {
				pOrderID = pOrderID.replace("DID-", "");
				logger.info("after replace orderID=" + pOrderID);
				String orderIds[] = pOrderID.split(",");
				for (String oID : orderIds) {
					OcOrder order = paymentService.findOrderByID(Integer.valueOf(oID));
					logger.info("order=" + order);
					logger.info("order.getDeviceId()=" + order.getDeviceId());
					logger.info("order.getPaperId()=" + order.getPaperId());
					if (order.getOrderId().startsWith("p")) {
						OcDevicePaper devicePaper = new OcDevicePaper();
						devicePaper.setDeviceId(order.getDeviceId());
						devicePaper.setPaperId(order.getPaperId());
						devicePaper.setCreateTime(timestamp);
						devicePaper.setUpdateTime(timestamp);
						paymentService.addDevicePaper(devicePaper);
					}
					else if (order.getOrderId().startsWith("v")) {
						paymentService.addVIP(order);
					}
					paymentService.payOrder(order, amount);
				}
			}
			else {
				paymentService.payOrder(pOrderID, amount);
				// 是购买试卷
				if (pOrderID!=null) {
					OcOrder order = paymentService.findOrder(pOrderID);
					if (pOrderID.startsWith("p")) {
						OcDevicePaper devicePaper = new OcDevicePaper();
						devicePaper.setDeviceId(order.getDeviceId());
						devicePaper.setPaperId(order.getPaperId());
						devicePaper.setCreateTime(timestamp);
						devicePaper.setUpdateTime(timestamp);
						paymentService.addDevicePaper(devicePaper);
					}
					else if (pOrderID.startsWith("v")) {
						paymentService.addVIP(order);
					}
				}
			}
			logger.info("order pay success. callbackInfo=" + pOrderID);
		}
		else {
			logger.info("order pay failure. callbackInfo is null.");
		}
		logger.info("write success to Youmi server.");
		response.getWriter().write("success");
		response.getWriter().flush();
		response.getWriter().close();
	}
	
}
